<template>
	<view class="container">
		<view class="tisbox">请认真填写信息方便确认</view>
		<form @submit="formSubmit" @reset="formReset">
			<view class="mapform">
				<view class="mapform_dl">
					<view class="mapform_dt">姓名：</view>
					<view class="mapform_dd"><input class="inp" type="text" @input="iname" :value="name"
							placeholder="请输入姓名" /></view>
				</view>
				<view class="mapform_dl">
					<view class="mapform_dt">联系电话：</view>
					<view class="mapform_dd"><input class="inp" type="text" @input="imobile" :value="mobile"
							placeholder="请输入联系电话" /></view>
				</view>
				<!-- <view class="mapform_dl">
					<view class="mapform_dt">邮箱：</view>
					<view class="mapform_dd"><input class="inp" type="text" @input="iemail" :value="email"
							placeholder="请输入邮箱" /></view>
				</view> -->
				
				<!-- <view class="mapform_dl">
					<view class="mapform_dt">性别：</view>
					<view class="mapform_dd">
						<picker @change="bindPickerChange1" :value="index1" :range="array1" range-key="name">
							<view class="picker">{{array1[index1].name}}</view>
						</picker>
						<view class="mapform_ico"></view>
					</view>
				</view>
				<view class="mapform_dl">
					<view class="mapform_dt">出生日期：</view>
					<view class="mapform_dd">
						<picker mode="date" value="date" start="1950-01-01" end="2030-12-31" @change="bindDateChange">
							<view class="picker">{{date}}</view>
						</picker>
						<view class="mapform_ico"></view>
					</view>
				</view> -->
				<view class="mapform_dl">
					<view class="mapform_dt">设置为默认预约人</view>
					<view class="mapform_dd">
						<switch @change="switch2Change" :checked="checked" />
					</view>
				</view>
			</view>
			<view class="formbottom">
				<view class="formbtn" @click="save">确定</view>
				<view class="formbtn del" v-if="id>0" @click="del">删除预约人</view>
			</view>
		</form>
	</view>
</template>

<script>
	import API from "@/common/api.js"
	export default {
		data() {
			return {
				array1: [{
					name: '男'
				}, {
					name: '女'
				}],
				index1: 0,
				name: "",
				mobile: "",
				email:"",
				date: "",
				checked: 0,
				id: 0

			}
		},
		onLoad(options) {
			if (options.id > 0) {
				this.id = options.id
				var $this = this
				API.post("common/addressDetail", {
					id: options.id
				}).then((res) => {
					if (res.code == 1) {
						$this.name = res.data.name
						$this.mobile = res.data.mobile
						if (res.data.sex == '女') {
							$this.index1 = 1;
						} else {
							$this.index1 = 0;
						}
						$this.date = res.data.birthday
						this.checked = res.data.mrdata
					}
				})
			}
		},
		methods: {
			del() {
				var $this = this
				uni.showModal({
					title: '',
					content: "确定要删除吗?",
					success: function(res) {
						if (res.confirm) {
							API.post("common/addressDel", {
								id: $this.id
							}).then((res) => {
								if (res.code == 1) {
									API.showModal(res.msg, "", "back")
								} else {
									API.showToast(res.msg)
								}
							})
						} else if (res.cancel) {
							console.log('用户点击取消');
						}
					}
				});
			},
			save() {
				if (!this.name) {
					API.showToast("请输入姓名")
				} else if (!(/^1[3456789]\d{9}$/.test(this.mobile))) {
					API.showToast("请输入手机号")
				}
				// else if (!this.email) {
				// 	API.showToast("请输入邮箱")
				// } 
				// else if (!this.date) {
				// 	API.showToast("请选择出生日期")
				// } 
				else {
					var json = {
						id: this.id,
						name: this.name,
						mobile: this.mobile,
						sex: this.array1[this.index1].name,
						birthday: this.date,
						email: this.email,
						check: this.checked
					}
					API.post("common/addAddress", json).then((res) => {
						if (res.code == 1) {
							API.showModal(res.msg, "/pages/user/jzradmin/jzradmin", "back")
						} else {
							API.showToast(res.msg)
						}
					})
				}
			},
			switch2Change() {
				if (this.checked) {
					this.checked = 0
				} else {
					this.checked = 1
				}
			},
			imobile(e) {
				this.mobile = e.detail.value
			},
			iname(e) {
				this.name = e.detail.value
			},
			iemail(e) {
				this.email = e.detail.value
			},
			bindPickerChange1: function(e) {
				console.log('picker发送选择改变，携带值为：' + e.detail.value)
				this.index1 = e.detail.value
			},
			bindDateChange: function(e) {
				this.date = e.detail.value
			},

		}
	}
</script>

<style lang="scss">
	page {
		background: #f7f7f7;
	}

	.tisbox {
		padding: 30rpx;
		color: #999;
		font-size: 28rpx;
	}
</style>